package com.gec.anan.map.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.gec.anan.common.execption.AnanException;
import com.gec.anan.common.result.ResultCodeEnum;
import com.gec.anan.map.service.MapService;
import com.gec.anan.model.form.map.CalculateDrivingLineForm;
import com.gec.anan.model.vo.map.DrivingLineVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class MapServiceImpl implements MapService {

    //由spring提供的远程调用工具
    @Autowired
    private RestTemplate restTemplate;
    @Value("${tencent.map.key}")//从yaml文件中拿出腾讯地图密钥
    private String key;


    @Override
    public DrivingLineVo calculateDrivingLine(CalculateDrivingLineForm calculateDrivingLineForm) {
        String url = "https://apis.map.qq.com/ws/direction/v1/driving/?from={from}&to={to}&key={key}";

        Map<String, String> map = new HashMap<>();
        map.put("from", calculateDrivingLineForm.getStartPointLatitude() + "," + calculateDrivingLineForm.getStartPointLongitude());
        map.put("to", calculateDrivingLineForm.getEndPointLatitude() + "," + calculateDrivingLineForm.getEndPointLongitude());
        map.put("key", key);

        JSONObject result = restTemplate.getForObject(url, JSONObject.class, map);
        System.out.println("result"+result.toString());
        if(result.getIntValue("status") != 0) {
            throw new AnanException(ResultCodeEnum.MAP_FAIL);
        }

        //返回第一条最佳线路
        JSONObject route = result.getJSONObject("result").getJSONArray("routes").getJSONObject(0);
        DrivingLineVo drivingLineVo = new DrivingLineVo();
        //单位：千米
        drivingLineVo.setDistance(route.getBigDecimal("distance").divide(new BigDecimal(1000)).setScale(2, RoundingMode.HALF_UP));
        drivingLineVo.setDuration(route.getBigDecimal("duration"));
        drivingLineVo.setPolyline(route.getJSONArray("polyline"));
        return drivingLineVo;

        //        //1.远程调用腾讯地图给的路线规划（腾讯云官方提供的接口）
//        String url = "https://apis.map.qq.com/ws/direction/v1/driving/?from={from}&to={to}&key={key}";
//        //2.补全参数
//        Map<String, String> map = new HashMap<>();
//        //起点的经纬度
//        map.put("from",calculateDrivingLineForm.getStartPointLatitude()+","+calculateDrivingLineForm.getStartPointLongitude());
//        //终点的经纬度
//        map.put("to", calculateDrivingLineForm.getEndPointLatitude()+","+calculateDrivingLineForm.getEndPointLongitude());
//        //腾讯云的key值
//        map.put("key", key);
//        //3.获取一整个对象{status:xxx,message:xxx,result:{},request_id:xxx}
//        JSONObject result = restTemplate.getForObject(url, JSONObject.class, map);
//        //判断调用是否成功(获取status的值)
//        int status = result.getIntValue("status");
//        if(status!=0){
//            throw new AnanException(ResultCodeEnum.MAP_FAIL );
//        }
//        //4.拿出对象中的数据(JSON格式的对象数据)
//        //需要返回路线数据(routes),选择第一条线路
//        JSONObject routes = result.getJSONObject("result").getJSONArray("routes").getJSONObject(0);
//        //5.创建vo对象
//        DrivingLineVo drivingLineVo = new DrivingLineVo();
//        //腾讯返回的单位为m，转化为km
//        //四舍五入，向上取整
//        drivingLineVo.setDistance(routes.getBigDecimal("distance")
//                .divide(new BigDecimal(1000))
//                .setScale(2,RoundingMode.HALF_UP));
//        drivingLineVo.setDuration(routes.getBigDecimal("duration"));
//        drivingLineVo.setPolyline(routes.getJSONArray("polyline"));
//        //6.返回vo对象数据
//        return drivingLineVo;
    }
}
